package com.prs.crm.action.person;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.JobTypeTitle;
import com.prs.crm.domain.person.Weave;
import com.prs.crm.service.person.WeaveService;

public class WeaveAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private Weave weave;
	private WeaveService weaveService;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Weave getWeave() {
		return weave;
	}
	public void setWeave(Weave weave) {
		this.weave = weave;
	}
	public WeaveService getWeaveService() {
		return weaveService;
	}
	public void setWeaveService(WeaveService weaveService) {
		this.weaveService = weaveService;
	}
	@Action("listWeave")
	public String listWeave(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Weave.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWeaveService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editWeave")
	public String editWeave(){
		if(ids!=null){
			this.weave=this.getWeaveService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveDeafult",results={
			@Result(name="success",type="redirect",location="listWeave"),
			@Result(name="error",type="chain",location="listWeave")
	})
	public String saveDeafult(){
		if(ids!=null){
			this.weave=this.getWeaveService().get(ids[0]);
		}
		weave.setType(1);
		this.getWeaveService().submit(weave);
		return SUCCESS;
	}
	
	@Action(value="saveDelDeafult",results={
			@Result(name="success",type="redirect",location="listWeave"),
			@Result(name="error",type="chain",location="listWeave")
	})
	public String saveDelDeafult(){
		List<Weave> list=this.getWeaveService().getAllWeave();
		for (int i = 0; i < list.size(); i++) {
			Weave job=list.get(i);
			if(job.getRemark()!=null){
				job.setType(2);
				this.getWeaveService().save(job);
			}
		}
		return SUCCESS;
	}
	
	@Action(value="saveWeave",results={
			@Result(name="success",type="redirect",location="listWeave"),
			@Result(name="error",type="chain",location="editWeave")
	})
	public String saveWeave(){
		weave.setType(2);
		this.getWeaveService().save(weave);
		return SUCCESS;
	}
	@Action(value="removeWeave",results={
			@Result(name="success",type="redirect",location="listWeave"),
			@Result(name="error",type="chain",location="listWeave")
	})
	public String removeWeave(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getWeaveService().batchRemove(ids);
		return SUCCESS;
	}
}
